import api from "@/api/apiConfig";
import ajax from "@/api/http";
import store from "@/store";
import router from "@/router";
import { notification } from "ant-design-vue";
/**
 * 树转数组扁平化结构
 * 深度优先遍历  递归
 */
export default {
  goPage(routerName, query) {
    router.push({
      name: routerName,
      query: query
    });
  },
  getAthList() {
    ajax
      .post(api.getUserInfo)
      .then(res => {
        let userInfo = {
          trueName: res.true_name,
          userName: res.username,
          mobile: res.mobile,
          avatar: res.avatar,
          id: res.id
        };
        store.dispatch("changeUserInfo", userInfo);
        store.dispatch("changeAthList", res.userAuthMenu);
      })
      .catch(err => {
        notification.error({
          message: err
        });
      });
  },
  // 判断权限
  isAth(key) {
    let list = store.getters.getChildAth;
    let keys = list.find(values => {
      return key === values;
    });
    if (keys) {
      return true;
    } else {
      return false;
    }
  },
  isPageAth(key) {
    let list = store.getters.getPageAth;
    let keys = list.find(values => {
      return key === values;
    });
    if (keys) {
      return true;
    } else {
      return false;
    }
  },
  // 截取长度为多少的字符串
  str(str, length) {
    var len = 0;
    let newStr = "";
    for (var i = 0; i < str.length; i++) {
      var c = str.charCodeAt(i);
      //单字节加1
      if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
        len++;
      } else {
        len += 2;
      }
      if (len < length) {
        newStr = newStr + str[i];
      } else {
        return newStr + "...";
      }
    }
    if (len < length) {
      return newStr;
    }
  },
  timer(data) {
    //毫秒转时间
    let times = new Date((data - 0) * 1000);
    let year = times.getFullYear();
    let month = "";
    let day = "";
    let hours = "";
    let minutes = "";
    let seconds = "";
    if (times.getMonth() + 1 <= 9) {
      month = "0" + (times.getMonth() + 1);
    } else {
      month = times.getMonth() + 1;
    }
    if (times.getDate() <= 9) {
      day = "0" + times.getDate();
    } else {
      day = times.getDate();
    }
    if (times.getHours() <= 9) {
      hours = "0" + times.getHours();
    } else {
      hours = times.getHours();
    }
    if (times.getMinutes() <= 9) {
      minutes = "0" + times.getMinutes();
    } else {
      minutes = times.getMinutes();
    }
    if (times.getSeconds() <= 9) {
      seconds = "0" + times.getSeconds();
    } else {
      seconds = times.getSeconds();
    }
    let date =
      year +
      "-" +
      month +
      "-" +
      day +
      " " +
      hours +
      ":" +
      minutes +
      ":" +
      seconds;
    return date;
  }
};
